package com.moxiao.yfjh.module.activity.dal.mysql.activityInfo;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.activity.controller.admin.activityInfo.vo.ActivityInfoExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityInfo.vo.ActivityInfoPageReqVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 相亲活动信息 Mapper
 */
@Mapper
public interface ActivityInfoMapper extends BaseMapperX<ActivityInfoDO> {

    default PageResult<ActivityInfoDO> selectPage(ActivityInfoPageReqVO reqVO) {
        LambdaQueryWrapperX<ActivityInfoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<ActivityInfoDO>();
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getNo, reqVO.getNo());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getActiveSource, reqVO.getActiveSource());
        lambdaQueryWrapperX.likeIfPresent(ActivityInfoDO::getName, reqVO.getName());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getActiveDate, reqVO.getActiveDate());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getActiveTimeBegin, reqVO.getActiveTimeBegin());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getActiveTimeEnd, reqVO.getActiveTimeEnd());
//                .eqIfPresent(ActivityInfoDO::getMenNum, reqVO.getMenNum())
//                .eqIfPresent(ActivityInfoDO::getWomenNum, reqVO.getWomenNum())
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getHead, reqVO.getHead());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getHeadSex, reqVO.getHeadSex());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getHeadUserId, reqVO.getHeadUserId());
        lambdaQueryWrapperX.likeIfPresent(ActivityInfoDO::getPackageName, reqVO.getPackageName());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getActiveCost, reqVO.getActiveCost());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getDeadline, reqVO.getDeadline());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getActiveDesc, reqVO.getActiveDesc());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getActiveCover, reqVO.getActiveCover());
        // 活动列表只显示审核通过的
        if (Objects .nonNull(reqVO.getIsPage()) && reqVO.getIsPage() == 1) {
            lambdaQueryWrapperX.eq(ActivityInfoDO::getAuditStatus, 1);
        } else {
            lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getAuditStatus, reqVO.getAuditStatus());
        }
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getStatus, reqVO.getStatus());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getAuditUser, reqVO.getAuditUser());
        lambdaQueryWrapperX.betweenIfPresent(ActivityInfoDO::getAuditTime, reqVO.getAuditTime());
        lambdaQueryWrapperX.eqIfPresent(ActivityInfoDO::getRejectReason, reqVO.getRejectReason());
        lambdaQueryWrapperX.betweenIfPresent(ActivityInfoDO::getCreateTime, reqVO.getCreateTime());

        lambdaQueryWrapperX.apply(reqVO.getProductType() != null && reqVO.getProductType() != "", "find_in_set('" + reqVO.getProductType() + "',product_type)");
        lambdaQueryWrapperX.orderByDesc(ActivityInfoDO::getId);
        return selectPage(reqVO, lambdaQueryWrapperX);
    }


    default PageResult<ActivityInfoDO> selectAppPage(ActivityInfoPageReqVO reqVO) {
        List<Byte> statusList = new ArrayList<>();
        // 活动状态合并后的结果
        if (reqVO.getAppType() == 2) {
            statusList.add((byte) 2);
        }
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityInfoDO>()
                .eqIfPresent(ActivityInfoDO::getNo, reqVO.getNo())
                .eqIfPresent(ActivityInfoDO::getActiveSource, reqVO.getActiveSource())
                .likeIfPresent(ActivityInfoDO::getName, reqVO.getName())
                .eqIfPresent(ActivityInfoDO::getActiveDate, reqVO.getActiveDate())
                .eqIfPresent(ActivityInfoDO::getActiveTimeBegin, reqVO.getActiveTimeBegin())
                .eqIfPresent(ActivityInfoDO::getActiveTimeEnd, reqVO.getActiveTimeEnd())
//                .eqIfPresent(ActivityInfoDO::getMenNum, reqVO.getMenNum())
//                .eqIfPresent(ActivityInfoDO::getWomenNum, reqVO.getWomenNum())
                .eqIfPresent(ActivityInfoDO::getHead, reqVO.getHead())
                .eqIfPresent(ActivityInfoDO::getHeadSex, reqVO.getHeadSex())
                .eqIfPresent(ActivityInfoDO::getHeadUserId, reqVO.getHeadUserId())
                .likeIfPresent(ActivityInfoDO::getPackageName, reqVO.getPackageName())
                .eqIfPresent(ActivityInfoDO::getActiveCost, reqVO.getActiveCost())
                .eqIfPresent(ActivityInfoDO::getDeadline, reqVO.getDeadline())
                .eqIfPresent(ActivityInfoDO::getActiveDesc, reqVO.getActiveDesc())
                .eqIfPresent(ActivityInfoDO::getActiveCover, reqVO.getActiveCover())
                .eqIfPresent(ActivityInfoDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(ActivityInfoDO::getAuditUser, reqVO.getAuditUser())
                .betweenIfPresent(ActivityInfoDO::getAuditTime, reqVO.getAuditTime())
                .eqIfPresent(ActivityInfoDO::getRejectReason, reqVO.getRejectReason())
//                .eqIfPresent(ActivityInfoDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityInfoDO::getCreateTime, reqVO.getCreateTime())
                .in(ActivityInfoDO::getStatus, statusList )

                .apply(reqVO.getProductType() != null && reqVO.getProductType() != "", "find_in_set('" + reqVO.getProductType() + "',product_type)")
                .orderByAsc(ActivityInfoDO::getStatus)
                .orderByAsc(ActivityInfoDO::getCreateTime));
    }


    /**
     * 创建人身份查询活动
     *
     * @param reqVO
     * @return
     */
    default PageResult<ActivityInfoDO> selectPageByHeaderId(ActivityInfoPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityInfoDO>()
                .eqIfPresent(ActivityInfoDO::getHeadUserId, reqVO.getUserId())
                .orderByDesc(ActivityInfoDO::getId));
    }

    /**
     * 参与人身份查询活动
     *
     * @param reqVO
     * @return
     */
    public List<ActivityInfoDO> selectPageByNotHeader(ActivityInfoPageReqVO reqVO);

    public Integer getInfoActivityInfoNotHeaderSum(ActivityInfoPageReqVO pageReqVO);

    default List<ActivityInfoDO> selectList(ActivityInfoExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ActivityInfoDO>()
                .eqIfPresent(ActivityInfoDO::getNo, reqVO.getNo())
                .eqIfPresent(ActivityInfoDO::getActiveSource, reqVO.getActiveSource())
                .likeIfPresent(ActivityInfoDO::getName, reqVO.getName())
                .eqIfPresent(ActivityInfoDO::getActiveDate, reqVO.getActiveDate())
                .betweenIfPresent(ActivityInfoDO::getActiveDate, reqVO.getActiveTime())
                .eqIfPresent(ActivityInfoDO::getActiveTimeBegin, reqVO.getActiveTimeBegin())
                .eqIfPresent(ActivityInfoDO::getActiveTimeEnd, reqVO.getActiveTimeEnd())
                .eqIfPresent(ActivityInfoDO::getMenNum, reqVO.getMenNum())
                .eqIfPresent(ActivityInfoDO::getWomenNum, reqVO.getWomenNum())
                .eqIfPresent(ActivityInfoDO::getHead, reqVO.getHead())
                .eqIfPresent(ActivityInfoDO::getHeadSex, reqVO.getHeadSex())
                .likeIfPresent(ActivityInfoDO::getProductType, reqVO.getProductType())
                .likeIfPresent(ActivityInfoDO::getPackageName, reqVO.getPackageName())
                .eqIfPresent(ActivityInfoDO::getActiveCost, reqVO.getActiveCost())

                .eqIfPresent(ActivityInfoDO::getDeadline, reqVO.getDeadline())
                .eqIfPresent(ActivityInfoDO::getActiveDesc, reqVO.getActiveDesc())
                .eqIfPresent(ActivityInfoDO::getActiveCover, reqVO.getActiveCover())
                .eqIfPresent(ActivityInfoDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(ActivityInfoDO::getAuditUser, reqVO.getAuditUser())
                .betweenIfPresent(ActivityInfoDO::getAuditTime, reqVO.getAuditTime())
                .eqIfPresent(ActivityInfoDO::getRejectReason, reqVO.getRejectReason())
                .eqIfPresent(ActivityInfoDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityInfoDO::getId));
    }

    @Select("select count(*) num from blind_activity_info where no = #{activeNo}")
    Integer getActiveNo(@Param("activeNo") String activeNo);

    /**
     * 获取用户性别
     */
    @Select("select sex  from blind_member_user where id = #{userId}")
    int getBlindUserSex(@Param("userId") Long userId);


    /**
     * 获取用户性别
     */
    @Select("select *  from blind_activity_info where no = #{no}")
    ActivityInfoDO getNo(@Param("no") String no);

    /**
     * 获得活动下所有商品的供货价之和
     */
    @Select("SELECT IFNULL(SUM(t2.supply_price) ,0.00)  " +
            " from blind_activity_info t " +
            " JOIN blind_activity_package t1 on t.id = t1.active_id " +
            " JOIN blind_product_info t2 on t1.product_id = t2.id and t1.merchant_id = t2.merchant_id" +
            " where t.id = #{activityId}")
    BigDecimal getSupplypriceAmount(@Param("activityId") Long activityId);

    /**
     * 获得未开始及报名中的活动
     *
     * @return
     */
    @Select("select * from blind_activity_info where deleted = 0 and  status in (2,3)")
    List<ActivityInfoDO> getTaskActivityList();


    public List<ActivityInfoDO> selectPageList(ActivityInfoPageReqVO reqVO);

    /**
     * 根据创建者查询已经创建得活动次数
     * 开始时间，结束时间
     */
    @Select("select IFNULL(count(*),0)  from blind_activity_info where deleted = 0 and status in (0,1,2,3,4,5,6) and head_user_id = #{id} and create_time between #{start} and #{end};")
    int getActivityNum(@Param("id") long id, @Param("start") String start, @Param("end") String end);
}
